<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;


use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\Image;
use AuctionCore\System\AuctionException;
use Think\Log;

class ClcwChipController extends BaseController
{
    private $empId;

    private $clcwChipLogic;

    public function __construct()
    {
        parent::__construct();

        $this->clcwChipLogic = D('clcwChip', 'Logic');

        $this->empId = $this->user_info['emp_id'];
        $this->assign("emp_id", $this->empId);
    }

    public function index()
    {
        $this->display();
    }

    /**
     * 碎片列表
     */
    public function clcwChipList()
    {
        $page = I("get.p", 1);
        $name = I("get.name", '', 'trim');
        $desc = I("get.desc", '', 'trim');
        $size = 10;
        $params = [];
        $params['desc'] = $desc;
        $params['name'] = $name;
        //获取list
        $result = $this->clcwChipLogic->getClcwChipList($page, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 添加碎片
     */
    public function addClcwChip()
    {
        $this->display();
    }

    public function addClcwChipData()
    {
        $name = I('post.name');
        if (empty($name)) {
            $this->helpJsonResult(400, "请填写标题");
        }
        $desc = I('post.desc');
        if (empty($desc)) {
            $this->helpJsonResult(400, "请填写描述");
        }
        $content = I('post.content');
        $jsonType = I('post.json_type');
        if ($jsonType == 1) {
            if (!$this->isJson($content)) {
                $this->helpJsonResult(400, "填写内容格式不正确");
            }
        }
        if (empty($content)) {
            $this->helpJsonResult(400, "请填写内容");
        }
        $data['name'] = $name;
        $data['desc'] = $desc;
        $data['content'] = $content;
        $res = $this->clcwChipLogic->addClcwChip($data);
        if ($res === false) {
            $this->helpJsonResult(500, "添加失败");
        }
        $this->helpJsonResult(200, "添加成功");
    }

    /**
     * 编辑碎片
     */
    public function editClcwChip()
    {
        $id = I('get.id');
        if ($id < 1) {
            $this->redirect('/ClcwChip/index', '', 3, '碎片信息错误');
        }
        $chip = $this->clcwChipLogic->getClcwChipInfoById($id);
        $this->assign('chip', $chip);
        $this->display();
    }

    public function editClcwChipData()
    {
        $id = I('post.id');
        $name = I('post.name', '', 'trim');
        if (empty($name)) {
            $this->helpJsonResult(400, "请填写标题");
        }
        $desc = I('post.desc', '', 'trim');
        if (empty($desc)) {
            $this->helpJsonResult(400, "请填写描述");
        }
        $content = I('post.content', '', 'trim');
        if (empty($content)) {
            $this->helpJsonResult(400, "请填写内容");
        }
        $jsonType = I('post.json_type/d', 0);
        if ($jsonType == 1) {
            if (!$this->isJson($content)) {
                $this->helpJsonResult(400, "填写内容格式不正确");
            }
        }
        $data['id'] = $id;
        $data['name'] = $name;
        $data['desc'] = $desc;
        $data['content'] = $content;
        $res = $this->clcwChipLogic->updateClcwChip($data);
        if ($res === false) {
            $this->helpJsonResult(500, "更新失败");
        }
        $this->helpJsonResult(200, "更新成功");

    }

    private function isJson($str)
    {
        $jObject = json_decode($str);
        return (is_object($jObject)) ? true : false;
    }

    public function addFile()
    {
        $pic = I('data.pic', '', '', $_FILES);
        $size = $pic['size'];
        if (!empty($pic)) {
            $flag = true;
            if ($pic['error'] != 0) {
                $flag = false;
                $rep['msg'] = '文件上传失败';
                Log::record($rep['msg'] . ',错误码：' . $pic['error'] . ',time:' . date('Y-m-d H:i:s'));
            }
            //图片上传
            if ($flag) {
                $object = 'upload/' . create_uuid() . '.' . get_extension($pic['name']);
                $pic_path = $pic['tmp_name'];
                try {
                    $aliObject = Image::upload_file_by_file(OSS_BUCKET, $object, $pic_path, null, '', 'chip');
                    if ($aliObject) {
                        $host = get_content_cdnurl();
                        $url = $host . $aliObject;
                    }
                    echo Util::auto_jsonp_or_json($_REQUEST, ['code' => 200, 'msg' => '', 'data' => $url]);
                    exit;
                } catch (AuctionException $e) {
                    echo Util::auto_jsonp_or_json($_REQUEST, ['msg' => $e->getMessage(), 'code' => $e->getCode()]);
                    exit;
                }
            }
        }
    }
}